#Author：雨霁夜茶

import requests
import json
import re
import time
import threading


data_value=0
data_dict={}
keys=set()
lock = threading.RLock()


headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
               'Accept - Encoding':'gzip, deflate, br',
               'Accept-Language':'zh-Hans-CN, zh-Hans; q=0.5',
               'Connection':'Keep-Alive',
                'Cookie':'__jsluid=6823e22e5056f8053057587328e8aadc; Hm_lvt_d461356e8fb2606677bc74b703fe784a=1532742105,1532914717; _ga=GA1.2.1801908393.1532742117; __guid=97746457.3196947643884655000.1540184219499.3843; lang=hk_CN; USER_UNI=fb076ec9ed3b3c70c2579013ba180e1c; PHPSESSID=ddfa59ca59e8bd366f5de3b9752c93fd; monitor_count=1',
               'Host':'www.bcex.ca',
               'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}

def getdata():
    return data_dict

def load_data():

        try:
            res = requests.get("https://www.bcex.ca/coins/markets?t=0.44346292413075594",headers = headers)
            lock.acquire()

            text = json.loads(res.text)

            count = len(text['data']['btc'])

            for i in range(count):
                content = text['data']['ckusd'][i]
                data_value = float(content['current'])

                if content['coin_to']=='ckusd': #btc   ckusd  eth usdt

                    key = content['coin_from'].lower()
                    keys.add(key)
                    if key == 'btc':
                        data_dict[key] = {'usdt': data_value, 'btc': 1, 'precent': 0}
                        btc_price = data_value
                        break

            for i in range(count):
                content = text['data']['ckusd'][i]
                data_value = float(content['current'])
                if content['coin_to'] == 'ckusd':  # btc   ckusd  eth usdt
                    key = content['coin_from'].lower()
                    keys.add(key)
                    data_dict[key]={'key':key,'usdt':data_value,'btc':0,'precent':0}

            for i in range(count):
                content = text['data']['btc'][i]
                data_value = float(content['current'])
                if content['coin_to'] == 'btc':  # btc   ckusd  eth usdt
                    key = content['coin_from'].lower()
                    keys.add(key)
                    if data_dict.get(key) != None:
                        data_dict.get(key)['btc'] = data_value
                    else:
                        data_dict[key] = {'key': key, 'usdt': data_value * btc_price, 'btc': data_value,
                                                  'precent': 0}

            data_dict['btc']['btc']=1
            lock.release()
            print('下载成功：Bcex')
            return data_dict, keys
        except requests.exceptions.RequestException:
            print('RequestException Bcex')
            return data_dict, keys
        except requests.exceptions.ConnectionError:
            print('ConnectionError Bcex')
            return data_dict, keys
        except :
            print('Error Bcex')
            return data_dict, keys



def loadingdata():
    t1 = threading.Thread(target=load_data)
    t1.start()

if __name__ == "__main__":
    loadingdata()
    time.sleep(8)
    for k,v in data_dict.items():
        print(k,':',v)